﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.OracleClient;

using Maticsoft.DBUtility;
using DQYT.HTDZ.Model;
using DQYT.HTDZ.IDAL;

namespace DQYT.HTDZ.OracleDAL
{
    public class BDRoleDAL : IBDRole
    {
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int JSE_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tb_sys_role");
            strSql.Append(" where JSE_ID=:JSE_ID ");
            OracleParameter[] parameters = {
					new OracleParameter(":JSE_ID", OracleType.Int32)			};
            parameters[0].Value = JSE_ID;

            return DbHelperOra.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(BDRoleModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tb_sys_role(");
            strSql.Append("JSE_ID,JSE_NAME,JSE_FLAG,JSE_DESCRIPTION)");
            strSql.Append(" values (");
            strSql.Append(":JSE_ID,:JSE_NAME,:JSE_FLAG,:JSE_DESCRIPTION)");
            OracleParameter[] parameters = {
					new OracleParameter(":JSE_ID", OracleType.Int32),
					new OracleParameter(":JSE_NAME", OracleType.NVarChar),
					new OracleParameter(":JSE_FLAG", OracleType.Int32),
					new OracleParameter(":JSE_DESCRIPTION", OracleType.NVarChar)};
            parameters[0].Value = model.JSE_ID;
            parameters[1].Value = model.JSE_NAME;
            parameters[2].Value = model.JSE_FLAG;
            parameters[3].Value = model.JSE_DESCRIPTION;

            int rows = DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(BDRoleModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tb_sys_role set ");
            strSql.Append("JSE_NAME=:JSE_NAME,");
            strSql.Append("JSE_FLAG=:JSE_FLAG,");
            strSql.Append("JSE_DESCRIPTION=:JSE_DESCRIPTION");
            strSql.Append(" where JSE_ID=:JSE_ID ");
            OracleParameter[] parameters = {
					new OracleParameter(":JSE_NAME", OracleType.NVarChar),
					new OracleParameter(":JSE_FLAG", OracleType.Int32),
					new OracleParameter(":JSE_DESCRIPTION", OracleType.NVarChar),
					new OracleParameter(":JSE_ID", OracleType.Int32)};
            parameters[0].Value = model.JSE_NAME;
            parameters[1].Value = model.JSE_FLAG;
            parameters[2].Value = model.JSE_DESCRIPTION;
            parameters[3].Value = model.JSE_ID;

            int rows = DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int JSE_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tb_sys_role ");
            strSql.Append(" where JSE_ID=:JSE_ID ");
            OracleParameter[] parameters = {
					new OracleParameter(":JSE_ID", OracleType.Int32)			};
            parameters[0].Value = JSE_ID;

            int rows = DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string JSE_IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tb_sys_role ");
            strSql.Append(" where JSE_ID in (" + JSE_IDlist + ")  ");
            int rows = DbHelperOra.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BDRoleModel GetModel(int JSE_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select JSE_ID,JSE_NAME,JSE_FLAG,JSE_DESCRIPTION from tb_sys_role ");
            strSql.Append(" where JSE_ID=:JSE_ID ");
            OracleParameter[] parameters = {
					new OracleParameter(":JSE_ID", OracleType.Int32)			};
            parameters[0].Value = JSE_ID;

            BDRoleModel model = new BDRoleModel();
            DataSet ds = DbHelperOra.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["JSE_ID"] != null && ds.Tables[0].Rows[0]["JSE_ID"].ToString() != "")
                {
                    model.JSE_ID = int.Parse(ds.Tables[0].Rows[0]["JSE_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["JSE_NAME"] != null && ds.Tables[0].Rows[0]["JSE_NAME"].ToString() != "")
                {
                    model.JSE_NAME = ds.Tables[0].Rows[0]["JSE_NAME"].ToString();
                }
                if (ds.Tables[0].Rows[0]["JSE_FLAG"] != null && ds.Tables[0].Rows[0]["JSE_FLAG"].ToString() != "")
                {
                    model.JSE_FLAG = int.Parse(ds.Tables[0].Rows[0]["JSE_FLAG"].ToString());
                }
                if (ds.Tables[0].Rows[0]["JSE_DESCRIPTION"] != null && ds.Tables[0].Rows[0]["JSE_DESCRIPTION"].ToString() != "")
                {
                    model.JSE_DESCRIPTION = ds.Tables[0].Rows[0]["JSE_DESCRIPTION"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select JSE_ID,JSE_NAME,JSE_FLAG,JSE_DESCRIPTION ");
            strSql.Append(" FROM tb_sys_role ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOra.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tb_sys_role ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperOra.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.JSE_ID desc");
            }
            strSql.Append(")AS Row, T.*  from tb_sys_role T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperOra.Query(strSql.ToString());
        }

        #endregion  Method

        #region 新添加的
        /// <summary>
        /// 获得查询数据
        /// </summary>
        /// <param name="strWhere">查询条件，jiaose j left join userinfo u </param>
        /// <returns>SELECT j.jse_id,j.jse_name,j.jse_description,count(u.use_id) as iNum</returns>
        public DataTable GetData(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT j.jse_id,j.jse_name,j.jse_description,count(u.use_id) as JSE_RS ");
            strSql.Append(" FROM tb_sys_role j left join tb_sys_user u on u.jse_id=j.jse_id ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" group by j.jse_id,j.jse_name,j.jse_description ");
            return DbHelperOra.Query(strSql.ToString()).Tables[0];
        }

        /// <summary>
        /// 获得下一个最大值
        /// </summary>
        /// <returns></returns>
        public int GetNextID()
        {
            return DbHelperOra.GetMaxID("JSE_ID", "tb_sys_role");
        }

        
        /// <summary>
        /// 获得记录数
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public bool CheckMC(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT count(jse_id) as iNum ");
            strSql.Append(" FROM tb_sys_role ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" WHERE jse_name='" + strWhere + "'");
            }
            object obj = DbHelperOra.GetSingle(strSql.ToString());
            if (obj.ToString() == "" || obj.ToString() == "0")
            {
                return false;
            }
            return true;
        }
        #endregion
    }
}
